package org.activequant.core.domainmodel.account;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.activequant.core.domainmodel.events.OrderCompletionEvent;
import org.activequant.core.domainmodel.events.OrderEvent;
import org.activequant.core.domainmodel.events.OrderUpdateEvent;

/**
 * Order history. Contains the order information, reference id, and list of order-related events:
 * executions, updates, etc. May represent still opened order
 * or completed order, depending on whether it has not-null <code>orderCompletion</code>.<br/>
 * Holds the following associated variables:
 * <ul>
 * <li>id(Long)</li>
 * <li>order(Order)</li>
 * <li>brokerAssignedId(String)</li>
 * <li>events(List&lt;OrderEvent&gt;)</li>
 * <li>completion(OrderCompletionEvent)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [22.11.2007] Created (Mike Kroutikov)<br>
 *
 *  @author Mike Kroutikov
 */
public class OrderHistory implements Serializable {
	
	private static final long serialVersionUID = -4836652792033841529L;
	/**
	 * private Long id;
	 */
	private Long id;
	/**
	 * private Order order;
	 */
	private Order order;
	/**
	 * private String brokerAssignedId = null;
	 */
	private String brokerAssignedId = null;
	/**
	 * private List&lt;OrderEvent&gt; events = new ArrayList&lt;OrderEvent&gt;();
	 */
	private List<OrderEvent> events = new ArrayList<OrderEvent>();
	/**
	 * private OrderCompletionEvent completion;
	 */
	private OrderCompletionEvent completion;
	/**
	 * returns the associated id(Long)
	 * @return
	 */
	public Long getId() {
		return id;
	}
	/**
	 * sets the associated id(Long) with the given id(Long)
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * returns whether the associated id(Long) is not <code>null</code>
	 * @return
	 */
	public boolean hasId() {
		return id != null;
	}
	
	/**
	 * Order value for this ticket.
	 * If there were any successful order updates, this will be
	 * the most recent (updated) version. The original version
	 * can be found by looking for {@link OrderUpdateEvent}s.<br/>
	 * returns the associated order(Order)
	 * @return
	 */
	public Order getOrder() {
		return order;
	}
	
	/**
	 * Sets the order.<br/>
	 * sets the associated order(Order) with the given val(Order)
	 * @param val order.
	 */
	public void setOrder(Order val) {
		order = val;
	}
	
	/**
	 * returns a string representation. <br/>
	 * <code>return order.toString() + "; " + (completion!=null?completion.toString():"");</code>
	 */
	public String toString(){
		return order.toString() + "; " + (completion!=null?completion.toString():"");
	}
	
	/**
	 * Broker assigned id.
	 * This id is generated by broker, and identifies this order ticket
	 * uniquely and persistently.<br/>
	 * returns the associated brokerAssignedId(String)
	 * @return broker assigned id.
	 */
	public String getBrokerAssignedId() {
		return brokerAssignedId;
	}
	
	/**
	 * Sets broker assigned id.<br/>
	 * sets the associated brokerAssignedId(String) with the given val(String)
	 * @param val id string.
	 */
	public void setBrokerAssignedId(String val) {
		brokerAssignedId = val;
	}

	/**
	 * List of all events in the chronological order.
	 * Note that the order is important for some reconstruction
	 * tasks (i.e. when trying to restore the series of updates that were
	 * applied to the order).<br/>
	 * returns the associated events(List&lt;OrderEvent&gt;)
	 * @return list of order events.
	 */
	public List<OrderEvent> getEvents() {
		return events;
	}
	
	/**
	 * Sets events.<br/>
	 * sets the associated events(List&lt;OrderEvent&gt;) with the given val(List&lt;OrderEvent&gt;)
	 * @param val event list.
	 */
	public void setEvents(List<OrderEvent> val) {
		events = val;
	}
	
	/**
	 * adds the given event(OrderEvent) to the associated events(List&lt;OrderEvent&gt;)
	 * @param event
	 */
	public void addEvent(OrderEvent event) {
		events.add(event);
	}
	
	/**
	 * Returns order ticket completion information. For opened orders, contains null.<br/>
	 * returns the associated completion(OrderCompletionEvent)
	 * @return completion info.
	 */
	public OrderCompletionEvent getHistoryCompletion() {
		return completion;
	}
	
	/**
	 * Sets order ticket completion info.<br/>
	 * sets the associated completion(OrderCompletionEvent) with the given val(OrderCompletionEvent)
	 * @param val
	 */
	public void setHistoryCompletion(OrderCompletionEvent val) {
		completion = val;
	}
}
